package com.syedu.day11;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Arrays;

//实现可克隆的标记接口
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp implements Cloneable {
    private int id;
    private String name;
    private int age;
    private Emp[] emps;

    //重写clone()方法,修改修饰符为public
    @Override
    public Object clone() throws CloneNotSupportedException {

        //调动父类(Object)的clone()方法,浅层复制
        //return super.clone();

        //自己写代码实现深层复制
        Emp emp = new Emp();
        emp.setId(id);
        emp.setName(name);
        emp.setAge(age);
        Emp[] newEmps = Arrays.copyOf(emps, emps.length);
        emp.setEmps(newEmps);
        return emp;
    }

    /**
     * 自动生成代码:
     * 方式一.通过开发工具快捷键自动生成构造方法,gets/set,hashCode,equals,toString...
     *   alt + insert
     *
     * 方式二:通过第三方组件lombok来自动产生构造方法,gets/set,hashCode,equals,toString...
     * 1.下载lombok jar包并且导入到当前项目中
     * 2.开发工具下载对应的lombok插件(新版idea自带这个插件)
     * 3.设置idea,勾选enable annotation processing,开启注解支持
     * 4.在需要产生代码的类上通过注解来实现自动产生代码
     *      @Data  ==  空参构造+get/set+hashCode+equals+toString
     *      @AllArgsConstructor == 全参构造
     *      @NoArgsConstructor == 空参构造
     *      @EqualsAndHashCode
     *      @ToString...
     */
   /* public Emp() {
    }

    public Emp(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Emp(int id, String name, int age, Emp[] emps) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.emps = emps;
    }

    public void setEmps(Emp[] emps) {
        this.emps = emps;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Emp[] getEmps() {
        return emps;
    }

    *//**
     * 重写equals()方法,把默认比较内存地址的功能重写成比较对象的属性值
     *//*
    *//*@Override
    public boolean equals(Object object) {
        //非空判断
        if (object == null) {
            return false;
        }
        //自反性判断
        if (this == object) {
            return true;
        }
        //强制类型转换
        Emp emp = (Emp) object;
        //判断对象中的值和原始对象的值是否相同
        *//**//*return emp.getId() == id && emp.getName().equals(name)
                && emp.getAge() == age;*//**//*

        return emp.getName().equals(name) && emp.getAge() == age;
    }

    //重写hashCode()方法
    @Override
    public int hashCode(){
        return name.hashCode()*31 + age;
    }*//*

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Emp emp = (Emp) o;
        return age == emp.age && Objects.equals(name, emp.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        String result = "工号:" + id + ",姓名:" + name + ",年龄:" + age;
        if (emps != null && emps.length > 0) {
            result += ",下属:" + Arrays.toString(emps);
        }
        return result;
    }*/


}